package com.bjpowernode.gateway.common.utils;

import cn.hutool.json.JSONUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * 快钱工具类
 */
public class KqUtil {

    /**
     * 添加param , 构建签名格式
     * @param returns
     * @param paramId
     * @param paramValue
     * @return
     */
    public static String appendParam(String returns, String paramId, String paramValue) {
        if (returns != "") {
            if (paramValue != "") {

                returns += "&" + paramId + "=" + paramValue;
            }
        } else {

            if (paramValue != "") {
                returns = paramId + "=" + paramValue;
            }
        }
        return returns;
    }

    /**
     * 支付查询demo
     */
    public static String kqQueryOrder(String userNo, String orderNo, String privateFile) {
        Map<String, Object> request = new HashMap<>();
        //固定值：1代表UTF-8;
        String inputCharset = "1";
        //固定值：v2.0 必填
        String version = "v2.0";
        //1代表Md5，2 代表PKI加密方式  必填
        String signType = "2";
        //人民币账号 membcode+01  必填
        String merchantAcctId = userNo;
        //固定值：0 按商户订单号单笔查询，1 按交易结束时间批量查询必填
        String queryType = "0";
        //固定值：1	代表简单查询 必填
        String queryMode = "1";
        String startTime = "";
        String endTime = "";
        String requestPage = "";
        String orderId = orderNo;
        String key = "27YKWKBKHT2IZSQ4";

        request.put("inputCharset", inputCharset);
        request.put("version", version);
        request.put("signType", signType);
        request.put("merchantAcctId", merchantAcctId);
        request.put("queryType", queryType);
        request.put("queryMode", queryMode);
        request.put("startTime", startTime);
        request.put("endTime", endTime);
        request.put("requestPage", requestPage);
        request.put("orderId", orderId);

        String message="";
        message = KqUtil.appendParam(message,"inputCharset",inputCharset);
        message = KqUtil.appendParam(message,"version",version);
        message = KqUtil.appendParam(message,"signType",signType);
        message = KqUtil.appendParam(message,"merchantAcctId",merchantAcctId);
        message = KqUtil.appendParam(message,"queryType",queryType);
        message = KqUtil.appendParam(message,"queryMode",queryMode);
        message = KqUtil.appendParam(message,"startTime",startTime);
        message = KqUtil.appendParam(message,"endTime",endTime);
        message = KqUtil.appendParam(message,"requestPage",requestPage);
        message = KqUtil.appendParam(message,"orderId",orderId);
        message = KqUtil.appendParam(message,"key",key);

        Pkipair pki = new Pkipair();
        String sign = pki.signMsg(message , privateFile);

        request.put("signMsg", sign);
        //sandbox提交地址
        String reqUrl = "https://sandbox.99bill.com/gatewayapi/gatewayOrderQuery.do";
        String response = "";
        try {
            // 使用httputil 工具直接发送请求 , 得到响应
            response = HttpUtil.doPostJsonRequestByHttps(JSONUtil.toJsonStr(request), reqUrl, 3000, 8000);
        } catch (Exception e) {
            response = "error";
            e.printStackTrace();
        }
        return response;
    }

}
